package com.campus.activity.controller;

import com.campus.activity.common.Result;
import com.campus.activity.entity.Activity;
import com.campus.activity.entity.Organizer;
import com.campus.activity.entity.Registration;
import com.campus.activity.entity.Feedback;
import com.campus.activity.service.ActivityService;
import com.campus.activity.service.OrganizerService;
import com.campus.activity.service.RegistrationService;
import com.campus.activity.service.FeedbackService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 组织者管理控制器
 * 
 * @author System
 * @since 2024
 */
@RestController
@RequestMapping("/organizer")
@Tag(name = "组织者管理", description = "组织者信息的增删改查管理")
@Validated
@CrossOrigin(origins = "*")
public class OrganizerController {

    @Autowired
    private OrganizerService organizerService;
    
    @Autowired
    private ActivityService activityService;
    
    @Autowired
    private RegistrationService registrationService;
    
    @Autowired
    private FeedbackService feedbackService;

    @PostMapping
    @Operation(summary = "添加组织者", description = "创建新的组织者信息")
    public Result<Boolean> addOrganizer(@Valid @RequestBody Organizer organizer) {
        boolean success = organizerService.addOrganizer(organizer);
        if (success) {
            return Result.success("组织者添加成功", true);
        } else {
            return Result.error("组织者添加失败，联系方式可能已存在");
        }
    }

    @PutMapping("/{organizerId}")
    @Operation(summary = "更新组织者信息", description = "更新指定组织者的信息")
    public Result<Boolean> updateOrganizer(
            @Parameter(description = "组织者ID") @PathVariable Integer organizerId,
            @Valid @RequestBody Organizer organizer) {
        boolean success = organizerService.updateOrganizer(organizerId, organizer);
        return success ? Result.success("组织者信息更新成功", true) : Result.error("组织者信息更新失败");
    }

    @DeleteMapping("/{organizerId}")
    @Operation(summary = "删除组织者", description = "删除指定的组织者")
    public Result<Boolean> deleteOrganizer(
            @Parameter(description = "组织者ID") @PathVariable Integer organizerId) {
        boolean success = organizerService.deleteOrganizer(organizerId);
        return success ? Result.success("组织者删除成功", true) : Result.error("组织者删除失败");
    }

    @GetMapping("/{organizerId}")
    @Operation(summary = "获取组织者详情", description = "根据组织者ID获取组织者详细信息")
    public Result<Organizer> getOrganizer(
            @Parameter(description = "组织者ID") @PathVariable Integer organizerId) {
        Organizer organizer = organizerService.getById(organizerId);
        return organizer != null ? Result.success(organizer) : Result.notFound();
    }

    @GetMapping("/name/{organizerName}")
    @Operation(summary = "根据姓名查询组织者", description = "根据组织者姓名模糊查询组织者列表")
    public Result<List<Organizer>> getOrganizersByName(
            @Parameter(description = "组织者姓名") @PathVariable String organizerName) {
        List<Organizer> organizers = organizerService.getOrganizersByName(organizerName);
        return Result.success(organizers);
    }

    @GetMapping("/contact/{contactInfo}")
    @Operation(summary = "根据联系方式查询组织者", description = "根据联系方式获取组织者信息")
    public Result<Organizer> getOrganizerByContact(
            @Parameter(description = "联系方式") @PathVariable String contactInfo) {
        Organizer organizer = organizerService.getOrganizerByContact(contactInfo);
        return organizer != null ? Result.success(organizer) : Result.notFound();
    }

    @GetMapping
    @Operation(summary = "获取所有组织者", description = "获取所有组织者列表")
    public Result<List<Organizer>> getAllOrganizers() {
        List<Organizer> organizers = organizerService.getAllOrganizers();
        return Result.success(organizers);
    }

    @GetMapping("/check-contact")
    @Operation(summary = "检查联系方式", description = "检查联系方式是否已存在")
    public Result<Boolean> checkContact(
            @Parameter(description = "联系方式") @RequestParam String contactInfo) {
        boolean exists = organizerService.isContactExists(contactInfo);
        return Result.success(exists);
    }

    @GetMapping("/activities")
    @Operation(summary = "获取组织者的活动", description = "获取指定组织者创建的所有活动")
    public Result<List<Activity>> getOrganizerActivities(
            @Parameter(description = "组织者ID") @RequestParam Integer organizerId) {
        List<Activity> activities = activityService.getActivitiesByOrganizerId(organizerId);
        return Result.success(activities);
    }

    @GetMapping("/statistics")
    @Operation(summary = "获取组织者统计", description = "获取组织者的活动统计信息")
    public Result<Map<String, Object>> getOrganizerStatistics(
            @Parameter(description = "组织者ID") @RequestParam Integer organizerId) {
        Map<String, Object> stats = new HashMap<>();
        
        List<Activity> activities = activityService.getActivitiesByOrganizerId(organizerId);
        long totalActivities = activities.size();
        long publishedActivities = activities.stream().filter(a -> "PUBLISHED".equals(a.getActivityType())).count();
        long draftActivities = activities.stream().filter(a -> "DRAFT".equals(a.getActivityType())).count();
        
        stats.put("totalActivities", totalActivities);
        stats.put("publishedActivities", publishedActivities);
        stats.put("draftActivities", draftActivities);
        stats.put("totalParticipants", 0); // 需要从注册服务获取
        
        return Result.success(stats);
    }

    // 注册管理接口
    @GetMapping("/registrations")
    @Operation(summary = "获取组织者活动的注册记录", description = "获取指定组织者所有活动的注册记录")
    public Result<List<Registration>> getOrganizerRegistrations(
            @Parameter(description = "组织者ID") @RequestParam Integer organizerId) {
        // 先获取组织者的所有活动
        List<Activity> activities = activityService.getActivitiesByOrganizerId(organizerId);
        List<Registration> allRegistrations = new java.util.ArrayList<>();
        
        // 获取每个活动的注册记录
        for (Activity activity : activities) {
            List<Registration> activityRegistrations = registrationService.getRegistrationsByActivityId(activity.getActivityId());
            allRegistrations.addAll(activityRegistrations);
        }
        
        return Result.success(allRegistrations);
    }

    @GetMapping("/registrations/{activityId}")
    @Operation(summary = "获取指定活动的注册记录", description = "获取组织者指定活动的注册记录")
    public Result<List<Registration>> getActivityRegistrations(
            @Parameter(description = "活动ID") @PathVariable Integer activityId) {
        List<Registration> registrations = registrationService.getRegistrationsByActivityId(activityId);
        return Result.success(registrations);
    }

    // 反馈管理接口
    @GetMapping("/feedbacks")
    @Operation(summary = "获取组织者活动的反馈", description = "获取指定组织者所有活动的反馈")
    public Result<List<Feedback>> getOrganizerFeedbacks(
            @Parameter(description = "组织者ID") @RequestParam Integer organizerId) {
        // 先获取组织者的所有活动
        List<Activity> activities = activityService.getActivitiesByOrganizerId(organizerId);
        List<Feedback> allFeedbacks = new java.util.ArrayList<>();
        
        // 获取每个活动的反馈
        for (Activity activity : activities) {
            List<Feedback> activityFeedbacks = feedbackService.getFeedbackInfo(activity.getActivityId());
            allFeedbacks.addAll(activityFeedbacks);
        }
        
        return Result.success(allFeedbacks);
    }

    @GetMapping("/feedbacks/{activityId}")
    @Operation(summary = "获取指定活动的反馈", description = "获取组织者指定活动的反馈")
    public Result<List<Feedback>> getActivityFeedbacks(
            @Parameter(description = "活动ID") @PathVariable Integer activityId) {
        List<Feedback> feedbacks = feedbackService.getFeedbackInfo(activityId);
        return Result.success(feedbacks);
    }
}